package com.das.mechanic_base.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3ScreenUtils;

public class X3ViewFinderView extends View {

    //相机遮罩框外面的线，阴影区域，滚动线
    private Paint area, line,tPaint,tPaint2;
    //相机遮罩框中间透明区域
    private Rect center;
    //屏幕大小
    private int screenHeight, screenWidth;

    private int lineX = 0;
    //滚动线速度
    private static final int SPEED = 5;

    private int textTop,textTop2;

    private String textStr;
    private String textStr2;
    private int centerLeft;
    private Bitmap scan_line;

    private Context mContext;
    private int centerTop;
    private Matrix matrix;

    private boolean isVertical = false;

    //是否是行驶证
    private boolean isDrivingLicense = false;

    public X3ViewFinderView(Context context) {
        super(context);
    }

    public X3ViewFinderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;

        textStr = mContext.getString(R.string.x3_recognize_car_number);

        area = new Paint(Paint.ANTI_ALIAS_FLAG);
        area.setStyle(Paint.Style.FILL);
        area.setColor(Color.parseColor("#99000000"));
        area.setAlpha(100);

        tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        tPaint.setColor(Color.WHITE);
        tPaint.setTypeface(Typeface.DEFAULT_BOLD);
        tPaint.setTextSize(X3ScreenUtils.dipToPx(18,context));

        tPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        tPaint2.setColor(Color.parseColor("#ffffff"));
        tPaint2.setTextSize(X3ScreenUtils.dipToPx(14,context));

        screenHeight = X3ScreenUtils.getScreenHeight(context);
        screenWidth = X3ScreenUtils.getScreenWidth(context);

        if (isDrivingLicense){
            center = getCenterRect(context, 200, 300);
        } else {
            center = isVertical == true ? getCenterRect(context, 280, 80) : getCenterRect(context, 80, 280);
        }

        line = new Paint(Paint.ANTI_ALIAS_FLAG);
        line.setStyle(Paint.Style.FILL);
        //line.setColor(Color.parseColor("#86FFE1"));

        if (isDrivingLicense){
            scan_line = BitmapFactory.decodeResource(getResources(), R.mipmap.x3_home_camera_line_drilic);
        } else {
            scan_line = BitmapFactory.decodeResource(getResources(), R.mipmap.x3_home_camera_line);
        }

        matrix = new Matrix();
    }

    public X3ViewFinderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 更换title
     * @param str
     */
    public void changeText(String str) {
        this.textStr = str;
        invalidate();
    }

    /**
     * 根据尺寸获取中间区大小
     *
     * @param context
     * @param height
     * @param width
     * @return
     */
    private Rect getCenterRect(Context context, int height, int width) {
        height = X3ScreenUtils.dipToPx(height,context);
        width = X3ScreenUtils.dipToPx(width,context);
        Rect rect = new Rect();
        centerLeft = (this.screenWidth - width) / 2;
        if (isDrivingLicense){
            centerTop = (this.screenHeight - height) / 2 ;
            textTop = centerTop - X3ScreenUtils.dipToPx(88,context);
            textTop2 = centerTop - X3ScreenUtils.dipToPx(46,context);
        } else {
            centerTop = isVertical ? (this.screenHeight - height) / 2 : (this.screenHeight - height) / 2 - height;
            textTop = centerTop - X3ScreenUtils.dipToPx(55,context);
            textTop2 = centerTop - X3ScreenUtils.dipToPx(25,context);
        }

        rect.set(centerLeft, centerTop, centerLeft + width, centerTop + height);
        return rect;
    }

    public void changeScanRect(boolean isVertical,boolean isInit) {
        this.isVertical = isVertical;
        isDrivingLicense = false;
        scan_line = BitmapFactory.decodeResource(getResources(), R.mipmap.x3_home_camera_line);
        textStr = mContext.getString(R.string.x3_recognize_car_number);
        textStr2 = "";
        if (!isVertical) {
            center = getCenterRect(getContext(), 80, 280);
            if (!isInit) {
                scan_line = adjustPhotoRotation(scan_line, -180);
            }
        } else {
            center = getCenterRect(getContext(), 280, 80);
            scan_line = adjustPhotoRotation(scan_line, 90);
        }
        invalidate();
    }

    public void changeDriLicScanRect(boolean isDrivingLicense) {
        this.isDrivingLicense = isDrivingLicense;
        if (isDrivingLicense){
            center = getCenterRect(getContext(), 200, 300);
            scan_line = BitmapFactory.decodeResource(getResources(), R.mipmap.x3_home_camera_line_drilic);
            textStr = mContext.getString(R.string.x3_car_dri_tips1);
            textStr2 = mContext.getString(R.string.x3_car_dri_tips2);
        }
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isDrivingLicense) {
            //绘制四周阴影区域（上下左右），注意+1和-1，不设置不显示边框
            canvas.drawRect(0, 0, screenWidth, center.top + 4 , area);
            canvas.drawRect(0, center.bottom + 2, screenWidth, screenHeight, area);
            canvas.drawRect(0, center.top + 4 , center.left + 0, center.bottom + 2, area);
            canvas.drawRect(center.right - 2, center.top + 4, screenWidth, center.bottom + 2, area);

            //缩放
            matrix.setTranslate(centerLeft - X3ScreenUtils.dipToPx(3, mContext), centerTop - X3ScreenUtils.dipToPx(0, mContext));
            canvas.drawBitmap(scan_line, matrix, tPaint);

            // 绘制文字
            Rect rect = new Rect();
            tPaint.getTextBounds(textStr, 0, textStr.length(), rect);
            int w = rect.width();
            canvas.drawText(textStr, (this.screenWidth - w) / 2, textTop, tPaint);

            // 绘制文字2
            Rect rect2 = new Rect();
            tPaint2.getTextBounds(textStr2, 0, textStr2.length(), rect2);
            int w2 = rect2.width();
            canvas.drawText(textStr2, (this.screenWidth - w2) / 2, textTop2, tPaint2);

            //滚动线
            lineX += SPEED;
            if (center.left + lineX >= center.right) {
                lineX = SPEED;
            }
            LinearGradient linearGradient = new LinearGradient(center.left + X3ScreenUtils.dipToPx(2, mContext), center.top + X3ScreenUtils.dipToPx(2, mContext), center.left + lineX, center.top,
                    new int[]{Color.parseColor("#10ffffff"), Color.parseColor("#8086FFE1")},
                    new float[]{0.5f, 1f}, Shader.TileMode.MIRROR);
            line.setShader(linearGradient);
            canvas.drawRect(center.left + X3ScreenUtils.dipToPx(8, mContext), center.top + + X3ScreenUtils.dipToPx(2, mContext), center.left + lineX - +X3ScreenUtils.dipToPx(5, mContext), center.bottom, line);
        } else {
            //绘制四周阴影区域（上下左右），注意+1和-1，不设置不显示边框
            canvas.drawRect(0, 0, screenWidth, center.top - 1, area);
            canvas.drawRect(0, center.bottom + 1, screenWidth, screenHeight, area);
            canvas.drawRect(0, center.top - 1, center.left - 1, center.bottom + 1, area);
            canvas.drawRect(center.right + 1, center.top - 1, screenWidth, center.bottom + 1, area);

            //缩放
            //Log.e("SSSS","是否被缩放了："+startScan);

            matrix.setTranslate(centerLeft - X3ScreenUtils.dipToPx(7, mContext), centerTop - X3ScreenUtils.dipToPx(10, mContext));
            canvas.drawBitmap(scan_line, matrix, tPaint);

            // 绘制文字
            Rect rect = new Rect();
            tPaint.getTextBounds(textStr, 0, textStr.length(), rect);
            int w = rect.width();
            canvas.drawText(textStr, (this.screenWidth - w) / 2, textTop, tPaint);

            //滚动线
            lineX += SPEED;

            if (isVertical) {
                if (center.top + lineX >= center.bottom) {
                    lineX = SPEED;
                }
                LinearGradient linearGradient = new LinearGradient(center.left, center.top, center.left, center.top + lineX,
                        new int[]{Color.parseColor("#10ffffff"), Color.parseColor("#8086FFE1")},
                        new float[]{0.5f, 1f}, Shader.TileMode.MIRROR);
                line.setShader(linearGradient);
                canvas.drawRect(center.left, center.top, center.right, center.top + lineX, line);
            } else {
                if (center.left + lineX >= center.right) {
                    lineX = SPEED;
                }
                LinearGradient linearGradient = new LinearGradient(center.left, center.top, center.left + lineX, center.top,
                        new int[]{Color.parseColor("#10ffffff"), Color.parseColor("#8086FFE1")},
                        new float[]{0.5f, 1f}, Shader.TileMode.MIRROR);
                line.setShader(linearGradient);
                canvas.drawRect(center.left, center.top, center.left + lineX, center.bottom, line);
            }
        }
        postInvalidate();
    }

    private Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {
        Matrix m = new Matrix();
        m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
        try {
            Bitmap bm1 = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true);
            return bm1;
        } catch (OutOfMemoryError ex) {
        }
        return null;
    }

    public boolean isDrivingLicense() {
        return isDrivingLicense;
    }

    public void setDrivingLicense(boolean drivingLicense) {
        isDrivingLicense = drivingLicense;
    }
}